﻿using System;
using System.Linq;
using AwesomeSlider.Models;
using AwesomeSlider.Services;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
namespace AwesomeSlider.Drivers
{
    public class SliderPartDriver : ContentPartDriver<SliderPart>
    {
        private ISliderItemService _sliderItemService;
        private IContentManager _contentManager;

        public SliderPartDriver(ISliderItemService sliderItemService, IContentManager contentManager)
        {
            _sliderItemService = sliderItemService;
            _contentManager = contentManager;
        }

        protected override DriverResult Display(SliderPart part, string displayType, dynamic shapeHelper)
        {
            if (displayType == "Detail")
            {
                return Combined(
                    ContentShape("Parts_Sliders_Slider",
                                     () => shapeHelper.Parts_Sliders_Slider(ContentPart: part)),
                    ContentShape("Parts_Sliders_Slider_Thumbs",
                                    () =>
                                    {
                                        var list = shapeHelper.List();
                                        list.AddRange(_sliderItemService.Get(part, VersionOptions.Published)
                                                          .Select<SliderItemPart, object>(bp => _contentManager.BuildDisplay(bp, "Summary")));
                                        return shapeHelper.Parts_Sliders_Slider_Thumbs(ContentPart: part, ContentItems: list);
                                    })
                );

            }

            return null;

        }

        //GET
        protected override DriverResult Editor(SliderPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_Sliders_Slider_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts.Sliders.Slider",
                    Model: part,
                    Prefix: Prefix));
        }

        //POST
        protected override DriverResult Editor(SliderPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }
    }
}